class GprofileController {
    

	def imageService

    static roleAccess =  [
            [action:["*"], roles:["admin"]],
        ]

    def index = {
        if (params["_action_Save"]) {
            save()
        }
        else if (params["_action_Update"]) {
            update()
        }
        else if (params["_action_Delete"]) {
            delete()
        } else {
                redirect(action:show,params:params)
        }
    }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [ save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ gprofileList: Gprofile.list( params ) ]
    }

    def show = {
        def gprofile = Gprofile.get( params.id )

        if(!gprofile) {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ gprofile : gprofile ] }
    }

    def delete = {
        def gprofile = Gprofile.get( params.id )
        if(gprofile) {
            gprofile.delete()
            flash.message = "Gprofile ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def gprofile = Gprofile.get( params.id )

        if(!gprofile) {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ gprofile : gprofile ]
        }
    }


    def update = {


        def gprofile = Gprofile.get( params.id )

        if(gprofile) {

/**
            def username = session.user?.name
            def file = params.imageFile
            println 'username=' + username
            println 'file='+file
            imageService.thumbnail(file, username+'.png', grailsApplication)
            params.imageName = username
*/

            gprofile.properties = params

            
		params.findAll{pm-> 
			pm.key.length()>2 && pm.key[0..2]=="otm"
		}.each {  p->
			def keys = p.key.split("_");
			def classkey = keys[1]
			def addtokeyorig = keys[2]
			def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
			classkey = classkey[0].toUpperCase() + classkey[1..-1]
		
			gprofile."$addtokeyorig"= null

			p.value.each { val->
				def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
				temp = temp.get(val)
				gprofile."$addtokey"(temp)			
			} 
		}


            if(!gprofile.hasErrors() && gprofile.save()) {
                flash.message = "Gprofile ${params.id} updated"
                redirect(action:show,id:gprofile.id)
            }
            else {
                render(view:'edit',model:[gprofile:gprofile])
            }
        }
        else {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def gprofile = new Gprofile()
        gprofile.properties = params
        return ['gprofile':gprofile]
    }

    def save = {
        def gprofile = Gprofile.get( params.id )

/*
        def username = session.user?.name
        def file = params['imageFile']
        imageService.thumbnail(file, username+'.png', grailsApplication)
        params.imageName = username
*/

	params.findAll{pm-> 
		pm.key.length()>2 && pm.key[0..2]=="otm"
	}.each {  p->
		def keys = p.key.split("_");
		def classkey = keys[1]
		def addtokeyorig = keys[2]
		def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
		classkey = classkey[0].toUpperCase() + classkey[1..-1]
	
		Gprofile."$addtokeyorig"= null

		p.value.each { val->
			def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
			temp = temp.get(val)
			Gprofile."$addtokey"(temp)			
		} 
	}


        if(!gprofile.hasErrors() && gprofile.save()) {
            flash.message = "Gprofile ${gprofile.id} created"
            redirect(action:show,id:gprofile.id)
        }
        else {
            render(view:'create',model:[gprofile:gprofile])
        }
    }
}
